
#include "tonecfg.h"
#include "dsk6416.h"
#include "dsk6416_aic23.h"

/* Codec configuration settings */

DSK6416_AIC23_CodecHandle hCodec;

DSK6416_AIC23_Config config = {
    0x001f,  // 0 DSK6416_AIC23_LEFTINVOL  Left line input channel volume
    0x001f,  // 1 DSK6416_AIC23_RIGHTINVOL Right line input channel volume
    0x00d8,  // 2 DSK6416_AIC23_LEFTHPVOL  Left channel headphone volume
    0x00d8,  // 3 DSK6416_AIC23_RIGHTHPVOL Right channel headphone volume
    0x0011,  // 4 DSK6416_AIC23_ANAPATH    Analog audio path control
    0x0000,  // 5 DSK6416_AIC23_DIGPATH    Digital audio path control
    0x0000,  // 6 DSK6416_AIC23_POWERDOWN  Power down control
    0x0043,  // 7 DSK6416_AIC23_DIGIF      Digital audio interface format
    0x0081,  // 8 DSK6416_AIC23_SAMPLERATE Sample rate control
    0x0001   // 9 DSK6416_AIC23_DIGACT     Digital interface activation
};


/*******************************************************************************
* FUNCTION : codec_io
*
* DESCRIPTION :
*   This function waits for a new stereo sample from the A/D and then sends a new stereo sample
*   to the codec D/A. Samples are of size 16 bit (short int).
*
* OUTPUT :
*   INT A/D sample
*
*******************************************************************************/

//void codec_io(int *datainL, int *datainR, int dataoutL, int dataoutR)
int codec_io(int dataoutL)
{
  Uint32 xL, xR;

  while(!DSK6416_AIC23_read(hCodec, &xL));  // read a sample from the left channel
  while(!DSK6416_AIC23_read(hCodec, &xR));  // read a sample from the right channel
  while(!DSK6416_AIC23_write(hCodec, dataoutL & 0x0ffff));  // write a sample to the left channel
//  while(!DSK6416_AIC23_write(hCodec, dataoutR & 0x0ffff));  // write a sample to the right channel
  while(!DSK6416_AIC23_write(hCodec, dataoutL & 0x0ffff));  // write a sample to the right channel
//  *datainL = (int)(short)(xL & 0x0ffff);  // two type casting are needed to achieve sign extension
//  *datainR = (int)(short)(xR & 0x0ffff);
//  return;
  return (int)(short)(xL & 0x0ffff);
}

/********************************************************************************/

void goto_idle()
{
	asm("	IDLE");
}
